package com.excel.poi.util;

import com.excel.annotation.AnnotationUtil;
import com.excel.common.AnalyticalData;
import com.excel.common.Constant;
import com.excel.exception.ExcelException;
import com.excel.poi.domain.PoiExcel;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.cglib.beans.BeanMap;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PoiUtil implements Constant {
    public PoiUtil() {
    }

    public static HSSFWorkbook createWorkbook(String sheetName, Class<?> className, Collection<Object> con) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
        HSSFWorkbook hwb = new HSSFWorkbook();
        createSheet(hwb, sheetName, className, con);
        return hwb;
    }

    public static void createSheet(HSSFWorkbook book, String sheetName, Class<?> className, Collection<Object> con) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
        HSSFSheet hSSFSheet = book.createSheet(sheetName != null ? sheetName : "sheet");
        Map<String, ?> titleMap = AnnotationUtil.getAnnotationTitleMap(className);
        if (titleMap != null && titleMap.size() != 0) {
            Set<String> key = titleMap.keySet();
            HSSFRow hSSFRow = hSSFSheet.createRow(0);
            hSSFRow.setHeight((short)350);
            Map<Integer, Object> positionMap = new HashMap<Integer, Object>(10);
            HSSFCellStyle titleCellStyle = (HSSFCellStyle)PoiExcel.createTitleCellStyle(book);
            HSSFCell cell = null;
            int column = 0;

            String beanMap;
            for(Iterator<String> it = key.iterator(); it.hasNext(); ++column) {
                beanMap = (String)it.next();
                hSSFSheet.setColumnWidth(column, 5000);
                positionMap.put(column, beanMap);
                cell = hSSFRow.createCell(column);
                cell.setCellStyle(titleCellStyle);
                setTitleCell(cell, (String)titleMap.get(beanMap));
            }

            if (con != null) {
                List<BeanMap> bodyMap = AnnotationUtil.getAnnotationData(className, con);
                beanMap = null;
                HSSFRow hSSFRowBody = null;

                for(int i = 0; i < bodyMap.size(); ++i) {
                    hSSFRowBody = hSSFSheet.createRow(i + 1);
                    BeanMap beanMap1 = (BeanMap)bodyMap.get(i);
                    Set<Integer> positionKey = positionMap.keySet();
                    Iterator<Integer> it = positionKey.iterator();

                    while(it.hasNext()) {
                        int num = (Integer)it.next();
                        cell = hSSFRowBody.createCell(num);
                        setTitleCell(cell, AnalyticalData.analyFieldType(beanMap1.get((String)positionMap.get(num))));
                    }
                }
            }

        } else {
            throw new ExcelException("Title can not be empty,Does not contain ExcelAnnotation!");
        }
    }

    private static void setTitleCell(Cell cell, String value) {
        cell.setCellType(1);
        cell.setCellValue(value);
    }

    public static XSSFWorkbook createXSSFWorkbook(String sheetName, Class<?> className, Collection<Object> con) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
        XSSFWorkbook hwb = new XSSFWorkbook();
        createXSSFSheet(hwb, sheetName, className, con);
        return hwb;
    }

    public static void createXSSFSheet(XSSFWorkbook book, String sheetName, Class<?> className, Collection<Object> con) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
        XSSFSheet hSSFSheet = book.createSheet(sheetName != null ? sheetName : "sheet");
        Map<String, ?> titleMap = AnnotationUtil.getAnnotationTitleMap(className);
        if (titleMap != null && titleMap.size() != 0) {
            Set<String> key = titleMap.keySet();
            XSSFRow hSSFRow = hSSFSheet.createRow(0);
            hSSFRow.setHeight((short)350);
            Map<Integer, Object> positionMap = new HashMap<Integer, Object>(10);
            CellStyle titleCellStyle = PoiExcel.createTitleCellStyle(book);
            XSSFCell cell = null;
            int column = 0;

            String beanMap;
            for(Iterator<String> it = key.iterator(); it.hasNext(); ++column) {
                beanMap = (String)it.next();
                hSSFSheet.setColumnWidth(column, 5000);
                positionMap.put(column, beanMap);
                cell = hSSFRow.createCell(column);
                cell.setCellStyle(titleCellStyle);
                setTitleCell(cell, (String)titleMap.get(beanMap));
            }

            if (con != null) {
                List<BeanMap> bodyMap = AnnotationUtil.getAnnotationData(className, con);
                beanMap = null;
                XSSFRow hSSFRowBody = null;

                for(int i = 0; i < bodyMap.size(); ++i) {
                    hSSFRowBody = hSSFSheet.createRow(i + 1);
                    BeanMap beanMap1 = (BeanMap)bodyMap.get(i);
                    Set<Integer> positionKey = positionMap.keySet();
                    Iterator<Integer> it = positionKey.iterator();

                    while(it.hasNext()) {
                        int num = (Integer)it.next();
                        cell = hSSFRowBody.createCell(num);
                        setTitleCell(cell, AnalyticalData.analyFieldType(beanMap1.get((String)positionMap.get(num))));
                    }
                }
            }

        } else {
            throw new ExcelException("Title can not be empty,Does not contain ExcelAnnotation!");
        }
    }

    public static void exportExcel(HttpServletResponse response, String excelName, Class<?> className, List<Object> dateList, Boolean var) {
        ServletOutputStream os = null;

        try {
            os = response.getOutputStream();
            Workbook book = createWorkbook(excelName, className, dateList);
            response.reset();
            response.setContentType("bin");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + excelName + ".xls\"");
            book.write(os);
            os.flush();
        } catch (Exception var14) {
            throw new ExcelException("exportExcel method error:" + var14.getMessage());
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException var13) {
                    throw new ExcelException("exportExcel method error:" + var13.getMessage());
                }
            }

        }

    }
}
